class GameUI extends eui.Component {
	private lb_scoreNum: eui.Label;
	private lb_timeNum: eui.Label;
	private group_gameOver: eui.Group;
	private lb_curScoreNum: eui.Label;
	private btn_replay: eui.Button;
	private lb_bestScoreNum: eui.Label;

	private group_effect: eui.Group;
	//private _bestScore:number=0;
	private _score: number = 0;
	private _time: number = 0;
	private game: Game;

	public constructor() {
		super();
		this.addEventListener(egret.Event.COMPLETE, () => {
			this.initState();
		}, this);
		this.skinName = "src/GameSkinUI.exml";
	}

	public initState() {
		this.Score = 0;
		this.Time = 10000;
		this.btn_replay.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick_replay, this);
	}

	private onClick_replay() {
		if (this.group_gameOver != null) {
			this.group_gameOver.visible = false;
			this.initState();
			this.game._actorsLayer.removeChildren();
			this.game._bulletsLayer.removeChildren();
		}
	}
	public set Score(value: number) {
		this._score = value;
		this.lb_scoreNum.text = value.toString();
	}

	public get Score(): number {
		return this._score;
	}


	public set Time(value: number) {
		this._time = value;
		// if (this._time <= 0) {
		// 	this.GameOver();
		// }
		var sec = Math.floor(value / 1000);
		var mSec = value % 1000;
		if (this.lb_timeNum != null) {
			this.lb_timeNum.text = (sec + "." + mSec).toString();
		}
	}

	public get Time(): number {
		return this._time;
	}

	public GameOver(game: Game) {
		if (this.group_gameOver != null) {
			this.group_gameOver.visible = true;
			this.lb_curScoreNum.text = this.lb_scoreNum.text;
			this.game = game;

			if (this.Score > this.getBestScore()) {
				this.setBestScore(this.Score);
			}
			this.lb_bestScoreNum.text = this.getBestScore().toString();
		}
	}

	public getBestScore(): number {
		var str = egret.localStorage.getItem("KZ_BestScore");
		if (str != null) {
			return parseInt(str);
		}
		return 0;
	}

	public setBestScore(value: number) {
		egret.localStorage.setItem("KZ_BestScore", value.toString());
	}

	public addScoreEffect(x: number, y: number, scoreNum: number) {
		var scoreEff = new eui.Label();
		scoreEff.x = x;
		scoreEff.y = y;
		scoreEff.text = scoreNum.toString();
		this.group_effect.addChild(scoreEff);

		var targetX = this.lb_scoreNum.x;
		var targetY = this.lb_scoreNum.y;
		egret.Tween.get(scoreEff)
			.to({ "x": targetX, "y": targetY }, 500, egret.Ease.cubicIn)
			.call(this.addScore, this, [scoreNum])
			.call(this.removeEffect, this, [scoreEff]);
	}

	public addScore(score: number) {
		this.Score += score;
	}

	public removeEffect(eff: egret.DisplayObject) {
		this.group_effect.removeChild(eff);
	}

	private mcFactory: egret.MovieClipDataFactory = null;
	public addAttackEffect(x: number, y: number) {
		if (this.mcFactory == null) {
			this.mcFactory = new egret.MovieClipDataFactory(RES.getRes("attack_effect_01_json"), RES.getRes("attack_effect_01_png"));
		}
		var movieClip = new egret.MovieClip(this.mcFactory.generateMovieClipData("attack_effect_01"));
		movieClip.x = x;
		movieClip.y = y;
		movieClip.gotoAndPlay("play", 1);
		this.group_effect.addChild(movieClip);
		egret.Tween.get(movieClip).wait(200).call(this.removeEffect, this, [movieClip]);
	}
} 